// 7.17.01 Dự trù thuốc, vật tư y
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-prototype-builtins */
import "./index.less";
import { useTranslation } from "react-i18next";
import PatientInformation from "@/components/ViewComponents/PatientInformation/index";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { useRef } from "react";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { admissionsCreate, dischargesCreate } from "@/api/SecondmentDictionary";
import { thIpmAdmissions } from "@/api/ImmigrationDictionary";
import { useMessage } from "@/hooks/web/useMessage";
const index = () => {
	const { t } = useTranslation();
	const { createMessage } = useMessage();
	const btnList = ["save", "transfer"];
	const formLeftFormSchema: FormSchema[] = [
		{
			label: t("felix_ui.admissionDate"),
			component: "DatePicker",
			field: "admissionDate" //入院日期
		},
		{
			label: t("felix_ui.dt_time"),
			component: "DatePicker",
			field: "dt_time" //时间
		},
		{
			label: t("felix_ui.received_from"),
			component: "Input",
			field: "received_from" //收到自
		},
		{
			label: t("felix_ui.referral_from"),
			component: "Input",
			field: "referral_from" //推荐来源
		},
		{
			label: t("felix_ui.name"),
			component: "Input",
			field: "name" //姓名
		},
		{
			label: t("felix_ui.departmentAdmissionDate"),
			component: "DatePicker",
			field: "departmentAdmissionDate" //部门录取日期
		},
		{
			label: t("felix_ui.department"),
			component: "Select",
			field: "deptId", //部门
			componentProps: {
				options: [
					{
						label: "暂无字典",
						value: "1"
					}
				]
			}
		},
		{
			label: t("felix_ui.pat_category"),
			component: "Select",
			field: "cateGory", //类别
			componentProps: {
				options: [
					{
						label: "暂无字典",
						value: "1"
					}
				]
			}
		},
		{
			label: t("felix_ui.pat_cardno"),
			component: "Input",
			field: "pat_cardno" //卡号
		},
		{
			label: t("felix_ui.date_from"),
			component: "DatePicker",
			field: "date_from" //起始日期
		},
		{
			label: t("felix_ui.date_to"),
			component: "DatePicker",
			field: "date_to" //迄今为止
		},
		{
			label: t("felix_ui.fam_relatives"),
			component: "Input",
			field: "fam_relatives" //相对
		},
		{
			label: t("felix_ui.address"),
			component: "Input",
			field: "address" //地址
		},
		{
			label: t("felix_ui.phone_numb"),
			component: "Input",
			field: "phone_numb" //电话号码
		},
		{
			label: t("felix_ui.assignedTransferLocation"),
			component: "Input",
			field: "assignedTransferLocation" //指定的转移地点
		},
		{
			label: t("felix_ui.assignedExaminationArea"),
			component: "Input",
			field: "assignedExaminationArea" //指定考试区域
		},
		{
			label: t("felix_ui.assignedTreatmentDepartment"),
			component: "Input",
			field: "assignedTreatmentDepartment" //指定治疗部门
		}
	];
	const formRightFormSchema: FormSchema[] = [
		{
			label: t("kevin_ui.discharge_date"),
			component: "DatePicker",
			field: "dcDtime" //出院时间
		},
		{
			label: t("felix_ui.department"),
			component: "Select",
			field: "deptId", //部门
			componentProps: {
				options: [
					{
						label: "暂无字典",
						value: "1"
					}
				]
			}
		},
		{
			label: t("felix_ui.bed"),
			component: "Input",
			field: "bed" //床
		},
		{
			label: t("felix_ui.result"),
			component: "Input",
			field: "result" //成果
		},
		{
			label: t("felix_ui.primary_diagnosis"),
			component: "Input",
			field: "primary_diagnosis" //初步诊断
		},
		{
			label: t("felix_ui.emgReason"),
			component: "Input",
			field: "emgReason" //原因
		},
		{
			label: t("felix_ui.comorbidities"),
			component: "Input",
			field: "comorbidities" //附带条件
		},
		{
			label: t("kevin_ui.attending_physician"),
			component: "Input",
			field: "attending_physician" //主治医生
		},
		{
			label: t("kevin_ui.condition"),
			component: "Input",
			field: "condition" //条件
		},
		{
			label: t("kevin_ui.referral"),
			component: "Input",
			field: "referral" //介绍
		},
		{
			label: t("kevin_ui.delivery_date"),
			component: "Input",
			field: "delivery_date" //交房日期
		},
		{
			label: t("felix_ui.remark"),
			component: "Input",
			field: "remark" //备注
		}
	];
	const [formLeftItems, { validateFields: validateFieldsAdmission }] = useForm({
		labelWidth: 120,
		schemas: formLeftFormSchema,
		baseColProps: { span: 12 }
	});
	const [formRightItems, { validateFields: validateFieldsDischarge }] = useForm({
		labelWidth: 120,
		schemas: formRightFormSchema,
		baseColProps: { span: 12 }
	});
	const patientInfoRef = useRef({
		patientId: ""
	});
	const rxIdRef = useRef("");

	/** Function */

	const patientSearchSuccess = (data: any) => {
		patientInfoRef.current = data;
		thIpmAdmissionsFn();
	};

	const handlerToolbarAction = (key: string) => {
		switch (key) {
			case "save":
				createAdmissionInfo();
				break;
			case "save_discharge":
				createDischargeInfo();
				break;
		}
	};

	/** Request */

	const createAdmissionInfo = async () => {
		try {
			const values = await validateFieldsAdmission();
			await admissionsCreate({
				...values,
				patId: patientInfoRef.current.patientId
				// rxId: "a1a9d531caf90c1a12891b308922d022"
			});
			createMessage.success("Add success");
		} catch (err) {
			createMessage.error("Add fail!");
		}
	};

	const createDischargeInfo = async () => {
		try {
			const formValue = await validateFieldsDischarge();
			await dischargesCreate({
				...formValue,
				adId: patientInfoRef.current.patientId
			});
			createMessage.success("Add success");
		} catch (err) {
			createMessage.error("Add fail!");
		}
	};

	const thIpmAdmissionsFn = async () => {
		const res = await thIpmAdmissions({ patientId: patientInfoRef.current.patientId });
		rxIdRef.current = res;
		console.log("诊断", res);
	};

	return (
		<div className="outpatient page">
			<div className="page-content flex flex-col">
				<div className="block flex-none">
					<PatientInformation successFn={patientSearchSuccess} />
				</div>
				<div className="form mt-2 flex-auto">
					<div className="left_form block">
						<div className="block-label">Ⅱ.{t("kevin_ui.INFORMATION")}</div>
						<BasicForm register={formLeftItems} labelWrap={true}></BasicForm>
					</div>
					<div className="right_form block ml-2">
						<div className="block-label">Ⅲ. {t("kevin_ui.discharge_information")}</div>
						<BasicForm register={formRightItems} labelWrap={true}></BasicForm>
					</div>
				</div>
			</div>
			<Toolbar btnList={btnList} action={handlerToolbarAction} />
		</div>
	);
};
export default index;
